package com.kust.smarthome.web.controller;

import com.kust.smarthome.web.dto.OrderDto;
import com.kust.smarthome.web.dto.OrderDto2;
import com.kust.smarthome.web.dto.Orderitem;
import com.kust.smarthome.web.model.Commodity;
import com.kust.smarthome.web.model.OrderForm;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.ICommodityService;
import com.kust.smarthome.web.service.IOrderFormService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/orderForm")
@Api(value = "订单相关操作", tags = {"shopping-tag"})
public class OrderFormController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private IOrderFormService orderFormService;

    @Resource
    private IUserService userService;

    /**
     * 当客户端点击购买操作之后,用户选择收货信息
     * 若收货信息为空则需要用户新建一个收货地址
     * 若不为空则发送"/commodity/buy"请求进入生成购买订单处
     */
    @PostMapping("/buy")
    @ApiOperation(value = "购买操作", notes = "客户端购买操作,传入商品id,返回bool值,为true则成功,前端跳转到付款页面")
    public boolean buy(Principal principal, OrderVo orderVo) {

        System.out.println(orderVo.toString());
        User user = userService.getByUsername(principal.getName());

        OrderForm orderForm = orderFormService.getById(orderVo.getId());

        if (user.getId().equals(orderForm.getUserId())) {
            orderForm.setAddressId(orderVo.getAddressId())
                    .setIntro(orderVo.getIntro());

            return orderFormService.updateById(orderForm);
        }

        return false;

    }

    /**
     * app 中需要用户在下单之后知道自己未付款的订单其中订单中状态为0,
     * @param principal 登录用户名
     * @return 返回未支付订单信息
     */
    @GetMapping("/getState0ByUser")
    @ApiOperation(value = "获取未付款订单信息")
    public List<OrderDto> getState0ByUser(Principal principal) {

        User user = userService.getByUsername(principal.getName());

        return orderFormService.getState0ByUserId(user.getId());

    }

    @GetMapping("/getOrderByID")
    @ApiOperation(value = "获取id订单信息")
    public OrderForm getOrderByID(Principal principal,String id) {

        User user = userService.getByUsername(principal.getName());

        return orderFormService.getOrderByID(id);

    }

    /**
     *
     * @param principal 登录用户名
     * @return 返回所有订单信息
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "获取所有订单信息")
    public List<OrderDto> getAll(Principal principal) {

        User user = userService.getByUsername(principal.getName());

        return orderFormService.getAll(user.getId());

    }


    @GetMapping("/getAll2")
    @ApiOperation(value = "获取所有订单信息")
    public List<OrderDto2> getAll2(Principal principal) {
        User user = userService.getByUsername(principal.getName());
        List<OrderDto2> orderlist = orderFormService.getAll2(user.getId());
        for (int i = 0; i < orderlist.size();i++) {
            String[] goodlist = orderlist.get(i).getCommodityId().split(";");
            String[] goodnumlist = orderlist.get(i).getCommodityNumber().split(";");
            Orderitem[] orderitems = new Orderitem[goodlist.length];
            for (int j = 0; j < goodlist.length;j++) {
                System.out.println(goodlist[j]);
                Commodity commodity = commodityService.getById(goodlist[j]);
                Orderitem orderitem = new Orderitem();
                orderitem.setCommodityNumber(goodnumlist[j])
                        .setCommodityId(goodlist[j])
                        .setImgurl1(commodity.getImgurl1())
                        .setName(commodity.getName())
                        .setIntro(commodity.getIntro())
                        .setMoney(commodity.getMoney());
                orderitems[j] = orderitem;
            }
            orderlist.get(i).setOrderitems(orderitems);
        }
        System.out.println(orderlist);
        return orderlist;

    }

    /**
     * 可接入支付宝支付接口,
     * @param id 通过 id 做操作
     * @return 操作从成功与否
     */
    @PostMapping("/pay")
    @ApiOperation(value = "支付操作", notes = "目前为假支付,后期APP出成品之后加入支付宝沙箱")
    public boolean payMoneyById(String id) {

        OrderForm orderForm = orderFormService.getById(id);
        orderForm.setState(1);
        orderFormService.updateById(orderForm);

        return true;
    }

    @PostMapping("/getState1ByUser")
    @ApiOperation(value = "获取state1", notes = "获取对应用户状中态为1(已付款,未发货)的列表")
    public List<OrderDto> getState1ByUser(Principal principal) {

        User user = userService.getByUsername(principal.getName());

        return orderFormService.getState1ByUserId(user.getId());
    }

    @PostMapping("/getState2ByUser")
    @ApiOperation(value = "获取state2", notes = "获取对应用户中状态为2(已发货)的列表")
    public List<OrderDto> getState2ByUser(Principal principal) {
        User user = userService.getByUsername(principal.getName());

        return orderFormService.getState2ByUserId(user.getId());
    }

    @PostMapping("/confirm")
    @ApiOperation(value = "确认收货")
    public boolean confirmReceipt(Principal principal, String id) {

        User user = userService.getByUsername(principal.getName());

        OrderForm orderForm = orderFormService.getById(id);

        if (orderForm.getUserId() == user.getId()) {
            orderForm.setState(3);
            orderFormService.updateById(orderForm);
            return true;
        }

        return false;
    }

    @PostMapping("/getState3ByUser")
    @ApiOperation(value = "获取state3", notes = "获取对应用户中状态为3(已签收,未评价)的列表")
    public List<OrderDto> getState3ByUser(Principal principal) {
        User user = userService.getByUsername(principal.getName());

        return orderFormService.getState3ByUserId(user.getId());
    }

    @PostMapping("/getState4ByUser")
    @ApiOperation(value = "获取state4", notes = "获取对应用户中状态为4(已评价)的列表")
    public List<OrderDto> getState4ByUser(Principal principal) {
        User user = userService.getByUsername(principal.getName());

        return orderFormService.getState4ByUserId(user.getId());
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除订单")
    public boolean deleteOrder(Principal principal, String id) {

        User user = userService.getByUsername(principal.getName());

        return orderFormService.deleteOrder(user.getId(),id);
    }

}

